package com.jiuyin.dianjing.ui.fragment.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.BannerModel;
import com.jiuyin.dianjing.model.GameInfoModel;
import com.jiuyin.dianjing.ui.activity.match.MatchCreateActivity;
import com.jiuyin.dianjing.ui.activity.match.MatchEnterActivity;
import com.jiuyin.dianjing.ui.activity.match.MatchMyActivity;
import com.jiuyin.dianjing.ui.activity.search.SearchMatchInfoActivity;
import com.jiuyin.dianjing.ui.activity.team.MyTeamsActivity;
import com.jiuyin.dianjing.ui.fragment.base.BaseTabFragment;
import com.jiuyin.dianjing.ui.fragment.match.AboutWarFragment;
import com.jiuyin.dianjing.ui.fragment.match.FragmentMatch;
import com.jiuyin.dianjing.ui.fragment.match.TrainingCampFragment;
import com.jiuyin.dianjing.util.LogUtil;
import com.jiuyin.dianjing.util.ToastUtil;
import com.lzy.okgo.cache.CacheMode;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bingoogolapple.bgabanner.BGABanner;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_DOWN_URL;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_GAME_ID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_GAME_LIST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_NAME;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_SEARCH;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_SRC;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_SRC_LIST;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_USERID;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_SEARCH_TYPE_MATCH;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_TYPE_HOT;
import static com.jiuyin.dianjing.api.enums.ApiEnum.APP_GET_GAME_API;
import static com.jiuyin.dianjing.api.enums.ApiEnum.APP_GET_VIEW_PAGE_API;
import static com.jiuyin.dianjing.constant.AppConstant.WEIBO_GONGYI;

/**
 * 主界面(赛事)
 */
public class FragmentSecond extends BaseTabFragment<GameInfoModel> {

    @BindView(R.id.banner_guide_content)
    BGABanner mBgaBanner;

    @Override
    public void fetchData() {
        ServerApi.post(APP_GET_VIEW_PAGE_API.getUrl(), null, CacheMode.REQUEST_FAILED_READ_CACHE, null, d -> addDisposable(d),
                new ApiResultCallback() {
                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        if (null != jsonObject && jsonObject.has(KEY_SRC_LIST)) {
                            LogUtil.log("FragmentSecond getViewPage onSuccess = " + jsonObject.toString());
                            JsonArray jsonArray = jsonObject.getAsJsonArray(KEY_SRC_LIST);
                            if (jsonArray != null) {
                                int size = jsonArray.size();
                                List<BannerModel> list = new ArrayList<>();
                                if (size > 0) {
                                    for (int i = 0; i < size; i++) {
                                        JsonObject object = jsonArray.get(i).getAsJsonObject();
                                        BannerModel bannerModel = new BannerModel();
                                        String src = object.get(KEY_SRC).getAsString();
                                        String down_url = object.get(KEY_DOWN_URL).getAsString();
                                        bannerModel.setSrc(src);
                                        bannerModel.setDown_url(down_url);
                                        list.add(bannerModel);
                                    }
                                    mBgaBanner.setData(list, null);
                                }
                            }
                        }
                    }

                    @Override
                    public void onFail(String msg) {

                    }

                    @Override
                    public void onError(String msg) {

                    }
                });

        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_TOKEN, HelperApplication.getToken());
        params.put(KEY_TYPE, VALUE_TYPE_HOT);
        JSONObject jsonObject = new JSONObject(params);
        ServerApi.post(APP_GET_GAME_API.getUrl(), jsonObject, CacheMode.NO_CACHE, null, d -> addDisposable(d),
                new ApiResultCallback() {
                    @Override
                    public void onSuccess(JsonObject jsonObject) {
                        if (null != jsonObject && jsonObject.has(KEY_GAME_LIST)) {
                            LogUtil.log("FragmentSecond getGame onSuccess = " + jsonObject.toString());
                            JsonArray jsonArray = jsonObject.getAsJsonArray(KEY_GAME_LIST);
                            if (jsonArray != null) {
                                int size = jsonArray.size();
                                List<GameInfoModel> list = new ArrayList<>();
                                if (size > 0) {
                                    for (int i = 0; i < size; i++) {
                                        JsonObject object = jsonArray.get(i).getAsJsonObject();
                                        GameInfoModel gameInfoModel = new GameInfoModel();
                                        String name = object.get(KEY_NAME).getAsString();
                                        String game_id = object.get(KEY_GAME_ID).getAsString();
                                        gameInfoModel.setGameId(game_id);
                                        gameInfoModel.setName(name);
                                        list.add(gameInfoModel);
                                    }
                                    addData(list);
                                }
                            }
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        LogUtil.log(msg);
                    }

                    @Override
                    public void onError(String msg) {
                        LogUtil.log(msg);
                    }
                });

    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_second;
    }

    @Override
    protected void initView() {
        mBgaBanner.setAdapter((BGABanner.Adapter<ImageView, BannerModel>) (banner, itemView, model, position) -> Glide.with(getContext())
                .load(model.getSrc())
                .placeholder(R.drawable.img_banner_placeholder)
                .into(itemView));
        mBgaBanner.setDelegate((BGABanner.Delegate<ImageView, BannerModel>) (banner, itemView, model, position) -> gotoBrowser(model.getDown_url()));
    }

    @OnClick({R.id.tv_search, R.id.tv_game_create, R.id.tv_game_enter, R.id.tv_my_game, R.id.tv_my_team, R.id.fab, R.id.tv_game_xly, R.id.tv_game_yz, R.id.tv_game_pl, R.id.tv_game_jqqd})
    public void onViewClicked(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_search:
                Bundle bundle = new Bundle();
                bundle.putString(KEY_SEARCH, getString(R.string.search_info_go));
                bundle.putString(KEY_TYPE, VALUE_SEARCH_TYPE_MATCH);
                goTargetActivity(SearchMatchInfoActivity.class, bundle);
                break;
            case R.id.tv_game_create:
                goTargetActivity(MatchCreateActivity.class);
                break;
            case R.id.tv_game_enter:
                goTargetActivity(MatchEnterActivity.class);
                break;
            case R.id.fab:
                Uri uri = Uri.parse(WEIBO_GONGYI);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                break;
            case R.id.tv_my_game:
                goTargetActivity(MatchMyActivity.class);
                break;
            case R.id.tv_my_team:
                Bundle bundle1 = new Bundle();
                bundle1.putString(KEY_TYPE, "1");
                bundle1.putString(KEY_USERID, "");
                goTargetActivity(MyTeamsActivity.class, bundle1);
                break;
            case R.id.tv_game_xly:
                startContainerActivity(TrainingCampFragment.class.getCanonicalName());
                break;
            case R.id.tv_game_yz:
                startContainerActivity(AboutWarFragment.class.getCanonicalName());
                break;
            case R.id.tv_game_pl:
                ToastUtil.showShort(R.string.training_jqqd);
//                startContainerActivity(TrainingCampFragment.class.getCanonicalName());
                break;
            case R.id.tv_game_jqqd:
                ToastUtil.showShort(R.string.training_jqqd);
//                startContainerActivity(TrainingCampFragment.class.getCanonicalName());
                break;
            default:
                break;
        }
    }


    @Override
    protected void addFragments(List<GameInfoModel> list) {
        for (GameInfoModel model : list) {
            mFragments.add(Pair.create(model.getName(), FragmentMatch.newInstance(model.getGameId(), true)));
        }
    }

    /**
     * 跳转浏览器
     */
    private void gotoBrowser(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri content_url = Uri.parse(url);
        intent.setData(content_url);
        startActivity(intent);
    }
}
